package com.dd.cloud.user.service.doctor.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.constants.UserConstants;
import com.dd.cloud.user.entity.doctor.DoctorDetailReview;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.user.mapper.doctor.DoctorDetailReviewMapper;
import com.dd.cloud.user.req.doctor.DoctorDetailApplyReviewReq;
import com.dd.cloud.user.res.doctor.DoctorDetailReviewDetailRes;
import com.dd.cloud.user.service.doctor.IDoctorDetailReviewService;
import com.dd.cloud.common.vo.LoginInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Lx
 * @version 1.0
 */
@Slf4j
@Service
public class DoctorDetailReviewServiceImpl extends ServiceImpl<DoctorDetailReviewMapper, DoctorDetailReview> implements IDoctorDetailReviewService {
    /**
     * 添加审核记录提示次数
     *
     * @param id
     * @param tipCount
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDoctorDetailReviewTipCount(Integer id, Integer tipCount, Integer pcTipCount) {
        UpdateWrapper<DoctorDetailReview> uw = new UpdateWrapper<>();
        uw.lambda().eq(DoctorDetailReview::getId, id);
        uw.lambda().set(DoctorDetailReview::getTipCount, tipCount + 1);
        if (pcTipCount != 0) {
            uw.lambda().set(DoctorDetailReview::getPcTipCount, pcTipCount);
        }
        update(uw);

    }

    /**
     * 添加审核记录
     *
     * @param req
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDoctorDetailReview(DoctorDetailApplyReviewReq req) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        DoctorDetailReview ddr = new DoctorDetailReview();
        ddr.setApplyId(req.getApplyId());
        ddr.setStatus(req.getStatus());
        ddr.setMsg(req.getMessage());
        ddr.setTipCount(0);
        ddr.setOperId(loginInfo.getId());
        ddr.setOperName(loginInfo.getName());
        ddr.setCreateDate(LocalDateTime.now());
        save(ddr);
    }

    /**
     * 查询申请记录审核信息
     *
     * @param applyId
     * @return
     */
    @Override
    public DoctorDetailReviewDetailRes queryDoctorDetailReviewDetail(Integer applyId) {
        DoctorDetailReviewDetailRes res = getBaseMapper().queryDoctorDetailReviewDetail(applyId);
        if (res == null) {
            List<DoctorDetailReviewDetailRes> resList = baseMapper.queryDoctorDetailReviewDetailList(applyId);
            if (CollectionUtils.isNotEmpty(resList) && resList.size() == 2 && resList.get(1).getStatus() == UserConstants.DoctorDetailApplyStatus.REVIEW_FA_IL
                    && resList.get(0).getType().equals(resList.get(1).getType())) {
                return resList.get(1);
            }
        }
        return res;
    }
}
